//
//  Stage01ViewController.m
//  NewGame
//
//  Created by wwj on 15/10/13.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "Stage01ViewController.h"
#import "SoundTool.h"
#import "IngScoreView.h"
#import "FeatherView.h"
#import "ReadyGoView.h"
#import "FeetView.h"
#import "StageModel.h"
#import "StageRecordModel.h"
#import "PauseViewController.h"

#define kRandomTime (1 + arc4random_uniform(3)/10)

@interface Stage01ViewController ()
@property (weak, nonatomic) IBOutlet FeetView *feetView;
@property (weak, nonatomic) IBOutlet FeatherView *featherView;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@property (strong, nonatomic) CADisplayLink *timeLink;

@property (assign, nonatomic) CGFloat leftTime;//计时器
@property (assign, nonatomic) CGFloat feetApperTime;

@end

@implementation Stage01ViewController

#pragma -mark 获取手势教程图片名称
-(NSArray *)getGuideImageName{
    return @[@"guide00", @"guide01"];
}

#pragma -mark 开始游戏
-(void)beginGame{
    [super beginGame];
    
    //设置分数初始参数
    self.score = 0;
    
    //倒计时总时间
    _leftTime = 7.0;
    _timeLabel.text = [NSString stringWithFormat:@"%.1f", _leftTime];
    //羽毛显示的时间
    _feetApperTime = kRandomTime;
    
    self.view.userInteractionEnabled = NO;
    
    //readGo
    [self.readyGoView beginWithBlock:^{
        //倒计时
        CADisplayLink *timeLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateTime:)];
        [timeLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
        _timeLink = timeLink;
    }];
     [_timeLink invalidate];
    //脚
    [_feetView begin];
    
    //羽毛
    [_featherView begin];
    
   
}

#pragma -mark 重新开始游戏
- (void)onClickRetry{
    [self beginGame];
}

#pragma -mark 暂停游戏
- (void)onClickPause{
    _timeLink.paused = YES;
    PauseViewController *pauseVC = [PauseViewController pauseViewController];
    pauseVC.pauseBlock = ^{
        [self.readyGoView beginWithBlock:^{
            _timeLink.paused = NO;
        }];
    };
    [self.navigationController pushViewController:pauseVC animated:YES];
}



#pragma -mark 结束游戏
-(void)endGame{
    [super endGame];
    [_timeLink invalidate];
}

- (IBAction)onClickBtn:(UIButton *)sender {
    int index = sender.frame.origin.x / sender.frame.size.width;
    //羽毛
    [_featherView attackWithIndex:index];
    
    //是否击中
    if ([_feetView isHitFeetWithIndex:index]) {
        self.score++;
        [self.ingScoreView addScoreWith:(self.score) stage:self.stageModel];
    }else{
        [self showMessWithBtn:sender];
    }
    [[SoundTool sharedSoundTool] playSound:kSoundTap];
}

#pragma -mark 显示Mess
-(void)showMessWithBtn:(UIButton *)btn{
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"01_miss"]];
    
    CGFloat imageW = 80;
    CGFloat imageH = 31;
    
    CGFloat imageX = CGRectGetMaxX(btn.frame) - imageW - 20;
    CGFloat imageY = CGRectGetMaxY(_feetView.frame) - 20;
    imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
    [self.view addSubview:imageView];
    
    [UIView animateWithDuration:1.0 animations:^{
        imageView.transform = CGAffineTransformMakeTranslation(0, -300);
        imageView.alpha = 0;
    } completion:^(BOOL finished) {
        [imageView removeFromSuperview];
    }];
}
     
#pragma 更新时间
-(void)updateTime:(CADisplayLink *)displayLink{
    
    self.view.userInteractionEnabled = YES;
    
    _feetApperTime -= displayLink.duration;
    
    _leftTime -= displayLink.duration;
    
    if (_feetApperTime <= 0 && _leftTime > 0) {
        [_feetView random];
        _feetApperTime = kRandomTime;
    }
    
    if (_leftTime <=0) {
        _leftTime = 0.0;
        [self endGame];
    }
    
    _timeLabel.text = [NSString stringWithFormat:@"%.1f", _leftTime];
}


@end
